package com.jimo.app.adp;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jimo.app.R;
import com.jimo.app.util.OnBack;

import java.util.ArrayList;
import java.util.List;
public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.ViewHolder> {
    private final List<PackageInfo> apps ;
    private final PackageManager pm;
    private final OnBack l;
    private final boolean selectInjected;
    private final Activity activity;

    public AppListAdapter(
            Activity activity,
            PackageManager pm, boolean selectInjected,ArrayList<PackageInfo> apps, OnBack l) {
        this.activity=activity;
        this.selectInjected = selectInjected;
        this.pm = pm;
        this.apps = apps;
        this.l=l;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PackageInfo app = apps.get(position);
        holder.appName.setText(pm.getApplicationLabel(app.applicationInfo));
        holder.appPackage.setText(app.packageName);
        holder.appIcon.setImageDrawable(pm.getApplicationIcon(app.applicationInfo));
        holder.showInjectedText.setVisibility(app.versionName.contains("inject_")?View.VISIBLE:View.GONE);
        holder.showInjectedView.setVisibility(app.versionName.contains("inject_")?View.VISIBLE:View.GONE);
        holder.view.setOnClickListener(v -> {
            boolean isInject=app.versionName.contains("inject_");
            if(selectInjected){
                if(isInject){
                    l.onBack(app);
                }else {
                    Toast.makeText(activity,"请选择已注入的app",Toast.LENGTH_SHORT).show();
                }
            }else{
                if(isInject){
                    Toast.makeText(activity,"请选择未注入的app",Toast.LENGTH_SHORT).show();
                }else {
                    l.onBack(app);
                }
            }

        });
    }

    @Override
    public int getItemCount() {
        return apps.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView appIcon;
        TextView showInjectedText;
        View showInjectedView;
        TextView appName;
        TextView appPackage;
        View view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            appIcon = itemView.findViewById(R.id.app_icon);
            appName = itemView.findViewById(R.id.app_name);
            appPackage = itemView.findViewById(R.id.app_package);
            showInjectedText = itemView.findViewById(R.id.showInjectedText);
            showInjectedView = itemView.findViewById(R.id.showInjectedView);
        }
    }
}
